<?php
    function getOpenid(){
        if(!$_GET['code']){
            //获取当前的url地址
            // $rUrl=_URL_.__ACTION__.'.html';
            $rUrl="www.ypyunedu.com/Home/Demo/index"; //改成当前的url
            $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="._APPID_."&redirect_uri=".$rUrl."&response_type=code&scope=snsapi_base&state=12345#wechat_redirect";
            //跳转页面
            redirect($url,0);
        }else{
            $aUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid="._APPID_."&secret="._APPSECRET_."&code=".$_GET['code']."&grant_type=authorization_code";
            //获取网页授权access_token和openid等
            $data=getHttp($aUrl);
            return $data['openid'];
        }
    }






    //获取用户详细信息
    function getUserInfo(){
        if(!$_GET['code']){
            //获取当前的url地址
            // $rUrl=_URL_.__ACTION__.'.html';
             $rUrl="http://www.ypyunedu.com/Home/Demo/index";//改成当前的url
            $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="._APPID_."&redirect_uri=".$rUrl."&response_type=code&scope=snsapi_userinfo&state=12345#wechat_redirect";
            //跳转页面
            redirect($url,0);
        }else{
            $getOpenidUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid="._APPID_."&secret="._APPSECRET_."&code=".$_GET['code']."&grant_type=authorization_code";
            //获取网页授权access_token和openid等
            $data=getHttp($getOpenidUrl);
            $getUserInfoUrl="https://api.weixin.qq.com/sns/userinfo?access_token=".$data['access_token']."&openid=".$data['openid']."&lang=zh_CN";
            //获取用户数据
            $userInfo=getHttp($getUserInfoUrl);
            //默认设置头像是132*132的
            $userInfo['headimgurl']=substr($userInfo['headimgurl'],0,strlen($userInfo['headimgurl'])-1);
            $userInfo['headimgurl']=$userInfo['headimgurl'].'132';
            $userInfo['openid']=$userInfo['openid'];
            $userInfo['city']=$userInfo['city'];
            $userInfo['province']=$userInfo['province'];
            $userInfo['country']=$userInfo['country'];
            $userInfo['userInfo']=$userInfo['userInfo'];
            $userInfo['nickname']=$userInfo['nickname'];
            $userInfo['sex']=$userInfo['sex'];
            // 将信息插入数据库
            $userInfo['addtime']=date("Y-m-d H:i:s");
            //删除language元素
            unset($userInfo['language']);
            $model=M("");
            if($model->table('wUserInfo')->data($userInfo)->add()){
                setSession($userInfo);
                session("status",null);
            }else{
                echo "验证错误";
            }
        }
    }


?>